package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDirtree;
import com.ruoyi.system.service.ISysDirtreeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 目录树Controller
 * 
 * @author ruoyi
 * @date 2019-11-11
 */
@Controller
@RequestMapping("/system/dirtree")
public class SysDirtreeController extends BaseController
{
    private String prefix = "system/dirtree";

    @Autowired
    private ISysDirtreeService sysDirtreeService;

    @RequiresPermissions("system:dirtree:view")
    @GetMapping()
    public String dirtree()
    {
        return prefix + "/dirtree";
    }

    /**
     * 查询目录树树列表
     */
    @RequiresPermissions("system:dirtree:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysDirtree> list(SysDirtree sysDirtree)
    {
        List<SysDirtree> list = sysDirtreeService.selectSysDirtreeList(sysDirtree);
        return list;
    }

    /**
     * 导出目录树列表
     */
    @RequiresPermissions("system:dirtree:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDirtree sysDirtree)
    {
        List<SysDirtree> list = sysDirtreeService.selectSysDirtreeList(sysDirtree);
        ExcelUtil<SysDirtree> util = new ExcelUtil<SysDirtree>(SysDirtree.class);
        return util.exportExcel(list, "dirtree");
    }

    /**
     * 新增目录树
     */
    @GetMapping(value = { "/add/{drrId}", "/add/" })
    public String add(@PathVariable(value = "drrId", required = false) Long drrId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(drrId))
        {
            mmap.put("sysDirtree", sysDirtreeService.selectSysDirtreeById(drrId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存目录树
     */
    @RequiresPermissions("system:dirtree:add")
    @Log(title = "目录树", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDirtree sysDirtree)
    {
        return toAjax(sysDirtreeService.insertSysDirtree(sysDirtree));
    }

    /**
     * 修改目录树
     */
    @GetMapping("/edit/{drrId}")
    public String edit(@PathVariable("drrId") Long drrId, ModelMap mmap)
    {
        SysDirtree sysDirtree = sysDirtreeService.selectSysDirtreeById(drrId);
        mmap.put("sysDirtree", sysDirtree);
        return prefix + "/edit";
    }

    /**
     * 修改保存目录树
     */
    @RequiresPermissions("system:dirtree:edit")
    @Log(title = "目录树", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDirtree sysDirtree)
    {
        return toAjax(sysDirtreeService.updateSysDirtree(sysDirtree));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:dirtree:remove")
    @Log(title = "目录树", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{drrId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("drrId") Long drrId)
    {
        return toAjax(sysDirtreeService.deleteSysDirtreeById(drrId));
    }

    /**
     * 选择目录树树
     */
    @GetMapping(value = { "/selectDirtreeTree/{drrId}", "/selectDirtreeTree/" })
    public String selectDirtreeTree(@PathVariable(value = "drrId", required = false) Long drrId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(drrId))
        {
            mmap.put("sysDirtree", sysDirtreeService.selectSysDirtreeById(drrId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载目录树树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(SysDirtree sysDirtree)
    {
        List<Ztree> ztrees = sysDirtreeService.selectSysDirtreeTree(sysDirtree);
        return ztrees;
    }


}
